package service

import (
	"duoke/model"
	"duoke/tables"
	"duoke/utils/gmd5"
	"duoke/utils/gstr"
	"duoke/utils/orm"
	"errors"
	"github.com/gin-gonic/gin"
	"time"
)

var GoodsAccount = new(goodsAccountService)

type goodsAccountService struct{}

// 账户信息
func (s *goodsAccountService) GetById(id int, field string) (*tables.Account, error) {
	account := new(tables.Account)
	has, err := orm.Db.ID(id).Cols(field).Get(account)
	if err != nil {
		return nil, err
	}
	if !has {
		return nil, errors.New("该账户不存在")
	}
	return account, nil
}

// 新增/保存
func (s *goodsAccountService) Save(c *gin.Context, req model.ReqGoodsAccount) error {
	accountR, _ := Request.Account(c)
	if req.UseStorageAcid > 0 {
		req.UseStorageAcid = accountR.Id
	}
	if req.Id != 0 {
		account, err := s.GetById(req.Id, "")
		if err != nil {
			return err
		}
		// 验证账户唯一性
		if req.Username != account.Username {
			total, err := orm.Db.Where("username =?", req.Username).Count(new(tables.Account))
			if err != nil {
				return err
			}
			if total > 0 {
				return errors.New("该用户名已被注册过账户")
			}
		}
		// 验证手机号码唯一性
		if req.Phone != account.Phone {
			total, err := orm.Db.Where("phone =?", req.Phone).Count(new(tables.Account))
			if err != nil {
				return err
			}
			if total > 0 {
				return errors.New("该手机号码已被注册过账户")
			}
		}
		// 密码加密
		if req.Password != "" {
			req.Password, _ = gmd5.Encrypt(req.Password + account.Salt)
		} else {
			req.Password = account.Password
		}
		// 保存数据
		_, err = orm.Db.ID(req.Id).Cols("password", "phone", "name", "nickname", "avatar", "expire_time", "has_apps", "use_storage_acid", "bind_goods_ids").Update(req)
		if err != nil {
			return err
		}
	} else {
		// 验证账户唯一性
		total, err := orm.Db.Where("username =?", req.Username).Count(new(tables.Account))
		if err != nil {
			return err
		}
		if total > 0 {
			return errors.New("该用户名已被注册过账户")
		}
		// 验证手机号码唯一性
		total, err = orm.Db.Where("phone =?", req.Phone).Count(new(tables.Account))
		if err != nil {
			return err
		}
		if total > 0 {
			return errors.New("该手机号码已被注册过账户")
		}
		// 密码加密
		req.Salt = gstr.Random(6)
		if req.Password != "" {
			req.Password, _ = gmd5.Encrypt(req.Password + req.Salt)
		}
		// 保存数据
		_, err = orm.Db.Insert(req)
		if err != nil {
			return err
		}
	}
	return nil
}

// 列表
func (s *goodsAccountService) List(c *gin.Context, req model.ReqList) ([]tables.Account, int64, error) {
	accountR, _ := Request.Account(c)
	// 分页
	offset := (req.PageNo - 1) * req.PageSize
	list := make([]tables.Account, 0)
	query := orm.Db.Desc("id").Where("id <>?", 1).Where("role =?", "goods").Where("pid =?", accountR.Id)
	if req.State != "" {
		query = query.Where("state =?", req.State)
	}
	if req.Keyword != "" {
		query = query.Where("username =? OR phone =? OR nickname =?", req.Keyword, req.Keyword, req.Keyword)
	}
	if req.IsExpire == "1" {
		query = query.Where("expire_time >?", time.Now().Unix())
	}
	if req.IsExpire == "0" {
		query = query.Where("expire_time <?", time.Now().Unix())
	}
	if req.GoodsId != "" {
		query = query.Where("FIND_IN_SET(" + req.GoodsId + ",bind_goods_ids)")
	}
	total, err := query.Limit(req.PageSize, offset).MustLogSQL().FindAndCount(&list)
	if err != nil {
		return nil, 0, err
	}
	return list, total, nil
}

// 列表
func (s *goodsAccountService) Del(req model.ReqIds) error {
	// 删除数据
	_, err := orm.Db.In("id", req.Ids).Delete(new(tables.Account))
	if err != nil {
		return err
	}
	return nil
}

// 状态
func (s *goodsAccountService) State(req model.ReqId) error {
	account, err := s.GetById(req.Id, "")
	if err != nil {
		return err
	}
	if account.State == 1 {
		account.State = 0
	} else {
		account.State = 1
	}
	_, err = orm.Db.ID(req.Id).Cols("state").Update(account)
	if err != nil {
		return err
	}
	return nil
}
